package com.dragon.app.marvel.core;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dragon.app.marvel2.NewMarvelCoreService;
import com.dragon.loan.entity.AcLoanApplyInfo;
import com.dragon.loan.service.LoanInfoIDragonService;
import com.dragon.rabbitmq.CoreServiceProduct;
import com.fintech.common.constant.ErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;

/**
 * 新核心系统跟老核心系统的对接扭转服务类
 * @author shan
 *
 */
@Service("newMarvelCoreService")
public class NewMarvelCoreServiceImpl implements NewMarvelCoreService{
	private final static String NEW_MARVEL_ON = "ON";
	private final static String NEW_MARVEL_CODE_SUCCESS = "success";
	private final static String NEW_MARVEL_CODE_ERROR = "error";
	private final static String NEW_MARVEL_CODE_FAIL = "fail";
	private final static String MATCH_FAIL = "-99";
	/** 是否进入新核心   0  否   1 是 */
	private final static Byte GO_NEW_MARVELS = new Byte("1");
	
	@Autowired
	private LoanInfoIDragonService loanInfoIDragonService;
	@Autowired
	private CoreServiceProduct coreServiceProduct;
	
	/**
	 * 新核心系统是否打开的一个开关
	 * @param  idCard 身份证号  如果传null的话 则只判断new_marvel_start 
	 * @param  loanId 贷款单号  
	 * @return  true 走新核心逻辑  false走旧核心逻辑
	 * @author laijingshan
	 * @date 2018年9月20日
	 */
	@Override
	public boolean isMarvelStart(String idCard, String loanId){
        //return StringUtils.equalsIgnoreCase(marvelStart, NEW_MARVEL_ON);
		return Boolean.TRUE;
		
	}
	
	/**
	 * 根据新核心的code 返回老核心的code
	 * @param newMarvelCode 新核心的code
	 * @return
	 * @author laijingshan
	 * @date 2018年9月20日
	 */
	@Override
	public String getCodeMapper(String newMarvelCode){
		if(StringUtils.equalsIgnoreCase(newMarvelCode, NEW_MARVEL_CODE_SUCCESS)){
			return ErrorCode.SUCCESS;
		}else if(StringUtils.equalsIgnoreCase(newMarvelCode, NEW_MARVEL_CODE_ERROR)){
			return ErrorCode.EXCEPTION;
		}else if(StringUtils.equalsIgnoreCase(newMarvelCode, NEW_MARVEL_CODE_FAIL)){
			return ErrorCode.FAIL;
		}
		return newMarvelCode;
	}
	
	/**
	 * 发送到新核心 （整合版本由于前段响应问题需要使用异步执行 AsyncCallApi）
	 * @param loanId 贷款单号  发送到新核心
	 * @return
	 * @author laijingshan
	 * @date 2018年9月27日
	 */
	@Deprecated
	@Override
	public void sendLoanApplyMessage(String loanId){
		try {
			if(isMarvelStart(null, loanId)){
				coreServiceProduct.sendLoanApplyMessage(loanId);
				AcLoanApplyInfo acLoanApplyInfo = new AcLoanApplyInfo();
				acLoanApplyInfo.setLoanId(loanId);
				acLoanApplyInfo.setGoNewMarvels(new Byte("1"));
				loanInfoIDragonService.input(acLoanApplyInfo);
				MarvelsLogUtil.getInstance().info("=======sendLoanApplyMessage=======" + loanId);
			}
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("=======sendLoanApplyMessage=======" + loanId, e);
		}
	}
}
